package com.leetcode.array.removeElement.demo2;

/**
 * @Author Zan
 * @Create 2024/5/8 10:57
 * @ClassName: Test
 * @Description : https://leetcode.cn/problems/remove-duplicates-from-sorted-array/description/  删除有序数组中的重复项
 */
public class Test {
    public static void main(String[] args) {
        Solution solution = new Solution();

        int[] nums = new int[]{0,0,1,1,1,2,2,3,3,4};

        int i = solution.removeDuplicates(nums);
        System.out.println(i);
    }
}

/**
 * 双指针，快指针去查，不重复就赋值给slow，重复就++，相当于将不重复的添加到最前面，最后返回slow即可
 */
class Solution {
    public int removeDuplicates(int[] nums) {
        int size = nums.length;
        if (size == 0) {
            return 0;
        }

        int slowIndex = 1;
        for (int fastIndex = 1; fastIndex < size; fastIndex++) {
            if (nums[fastIndex - 1] != nums[fastIndex]) {
                nums[slowIndex] = nums[fastIndex];
                slowIndex++;
            }
        }
        return slowIndex;
    }
}